package com.xcfw.dorm.controller.dormcenter;

import com.xcfw.dorm.controller.BaseController;
import com.xcfw.dorm.entity.Building;
import com.xcfw.dorm.entity.NumEnum;
import com.xcfw.dorm.entity.ReturnJson;
import com.xcfw.dorm.exception.ServiceException;
import com.xcfw.dorm.exception.ValidationException;
import com.xcfw.dorm.sevice.ZSGCService;
import com.xcfw.dorm.seviceimpl.dormcenter.BuildingServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: Dorm
 * @description: 楼栋
 * @author: Bruse Queen
 * @create: 2018-04-07 16:41
 **/
@RestController
@Slf4j
@RequestMapping(value = "/dormcenter")
@CrossOrigin
public class BuildingController extends BaseController {

    @Autowired
    private BuildingServiceImpl zsgcService;

    @RequestMapping(value = "/addBuilding", method = RequestMethod.POST)
    public ReturnJson addBuilding(@RequestBody Building building) {
        try {
            zsgcService.add(building);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "楼栋信息新增成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("BuildingController---addBuilding--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/deleteBuilding", method = RequestMethod.POST)
    public ReturnJson deleteAssistant(@RequestBody Building building) {
        try {
            zsgcService.delete(building.getId());
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "楼栋信息删除成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("BuildingController---deleteBuilding--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/updateBuilding", method = RequestMethod.POST)
    public ReturnJson updateAssistant(@RequestBody Building building) {
        try {
            zsgcService.update(building);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "楼栋信息修改成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("BuildingController---updateBuilding--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/selectBuilding", method = RequestMethod.POST)
    public ReturnJson selectAssistant(@RequestBody Building building) {
        try {
            List<Building> list = (List<Building>) zsgcService.select(building);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "楼栋信息查询成功！", list);
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("BuildingController---selectBuilding--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/getBuildingJson", method = RequestMethod.POST)
    public ReturnJson getBuildingJson(@RequestBody Building building) {
        try {
            List<Object> list = (List<Object>) zsgcService.getBuildingJson(building);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "楼栋信息查询成功！", list);

        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("BuildingController---selectBuilding--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }
}
